package com.czy.owner.adapter;

import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;


import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.czy.owner.R;
import com.czy.owner.entity.Level0Item;
import com.czy.owner.entity.Level1Item;
import com.czy.owner.entity.Person;
import com.czy.owner.entity.TestingDetailsModel;
import com.czy.owner.utils.PhoneUtils;
import com.czy.owner.utils.StringUtils;
import com.czy.owner.widget.ExpandImageView;


import java.util.List;

/**
 * Created by hedepu on 2017/10/27.
 */
public class ExpandableItemAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> {
    private static final String TAG = ExpandableItemAdapter.class.getSimpleName();

    public static final int TYPE_LEVEL_0 = 0;
    public static final int TYPE_LEVEL_1 = 1;
    public static final int TYPE_PERSON = 2;

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public ExpandableItemAdapter(List<MultiItemEntity> data) {
        super(data);
        addItemType(TYPE_LEVEL_0, R.layout.item_expandable_lv0);
        addItemType(TYPE_LEVEL_1, R.layout.item_expandable_lv1);
        addItemType(TYPE_PERSON, R.layout.item_expandable_lv2);
    }


    @Override
    protected void convert(final BaseViewHolder holder, final MultiItemEntity item) {
        switch (holder.getItemViewType()) {
            case TYPE_LEVEL_0:
//                switch (holder.getLayoutPosition() %
//                        3) {
//                    case 0:
//                        holder.setImageResource(R.id.iv_head, R.mipmap.app_icon);
//                        break;
//                    case 1:
//                        holder.setImageResource(R.id.iv_head, R.mipmap.app_icon);
//                        break;
//                    case 2:
//                        holder.setImageResource(R.id.iv_head, R.mipmap.app_icon);
//                        break;
//                }
                final TestingDetailsModel.CategoryModel lv0 = (TestingDetailsModel.CategoryModel) item;
                holder.setText(R.id.title, lv0.getCarInspectionCategoryName())
                        .setImageResource(R.id.iv, lv0.isExpanded() ? R.mipmap.expand_top_two : R.mipmap.expand_top_two);
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int pos = holder.getAdapterPosition();

                        if (lv0.isExpanded()) {
                            collapse(pos);
                        } else {
//                            if (pos % 3 == 0) {
//                                expandAll(pos, false);
//                            } else {
                            expand(pos);
//                            }
                        }
                    }
                });
                break;
            case TYPE_LEVEL_1:
                final TestingDetailsModel.InspectionModel lv1 = (TestingDetailsModel.InspectionModel) item;
                holder.setText(R.id.title, lv1.getCarInspectionName())
                        .setImageResource(R.id.iv, lv1.isExpanded() ? R.mipmap.expand_top_two : R.mipmap.expand_bottom_two);
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int pos = holder.getAdapterPosition();

                        if (lv1.isExpanded()) {
                            collapse(pos, false);
                        } else {
                            expand(pos, false);
                        }
                    }
                });

//                holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
//                    @Override
//                    public boolean onLongClick(View v) {
//                        int pos = holder.getAdapterPosition();
//                        remove(pos);
//                        return true;
//                    }
//                });
                break;
            case TYPE_PERSON:
                final TestingDetailsModel.ItemModel person = (TestingDetailsModel.ItemModel) item;
                TextView tv_title = (TextView) holder.getView(R.id.tv_title);
                if ("abnormal".equals(person.getStatus())) {
                    holder.getView(R.id.img_left).setVisibility(View.VISIBLE);
                    holder.getView(R.id.view_left).setVisibility(View.GONE);

                    tv_title.setTextColor(mContext.getResources().getColor(R.color.order_time));
                } else {
                    holder.getView(R.id.img_left).setVisibility(View.GONE);
                    holder.getView(R.id.view_left).setVisibility(View.VISIBLE);
                    tv_title.setTextColor(mContext.getResources().getColor(R.color.appThemeTextColorBlack));
                }
                TextView tv_remarks = (TextView) holder.itemView.findViewById(R.id.tv_remarks);
                LinearLayout linearLayout = (LinearLayout) holder.itemView.findViewById(R.id.linear_add_content);
                linearLayout.removeAllViews();
                if (StringUtils.isEmpty(person.getReferenceStandard())) {
                    tv_remarks.setVisibility(View.GONE);
                } else {
                    tv_remarks.setVisibility(View.VISIBLE);
                    tv_remarks.setText(person.getReferenceStandard());
                }
                String text = "";
                for (TestingDetailsModel.ItemValueModel type : person.getItemValueList()) {
                    switch (person.getChooseType()) {
                        //单选
                        case "single":
                            if ("true".equals(type.getVal()))
                                text = text + type.getCarInspectionItemValue() + " ";
                            break;
                        //多选
                        case "multiple":
                            if ("true".equals(type.getVal()))
                                text = text + type.getCarInspectionItemValue() + " ";
                            break;
                        //文本
                        case "text":
                            TextView textView = new TextView(mContext);
                            textView.setTextAppearance(mContext, R.style.CommonTextViewThreeTitle);
                            textView.setPadding(0, PhoneUtils.dip2px(mContext, 3), 0, PhoneUtils.dip2px(mContext, 3));
                            textView.setText(type.getCarInspectionItemValue() + "：" + type.getVal());
                            linearLayout.addView(textView);
                            break;
                    }
                }
                holder.setText(R.id.tv_title, person.getCarInspectionItemName() + (StringUtils.isEmpty(text) ? "" : "：" + text));
                //是否添加图片
                if (person.getOwnerInspectionResList() != null && person.getOwnerInspectionResList().size() > 0) {
                    ExpandImageView expandImageView = new ExpandImageView(mContext, person.getOwnerInspectionResList());
                    linearLayout.addView(expandImageView);
                }
//                holder.itemView.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View view) {
//                        int pos = holder.getAdapterPosition();
//                        remove(pos);
//                    }
//                });
                break;
        }
    }
}
